package com.hdax.gulimall.order.interceptor;

import com.hdax.common.constant.AuthConstant;
import com.hdax.common.to.MemberEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class LoginUserInterceptor implements HandlerInterceptor {


    public static ThreadLocal<MemberEntity> local=new ThreadLocal<>();

    /**
     * 业务执行前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        boolean match = new AntPathMatcher().match("/pay/notify", request.getRequestURI());
        if(match){
            return true;
        }
        if("aa".equals(request.getHeader("__token"))){
           return true;
       }

        MemberEntity attribute = (MemberEntity) request.getSession().getAttribute(AuthConstant.LOGIN_USER);
        if(attribute!=null){
            local.set(attribute);
            return true;
        }else{
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
